package com.zhangmeng.service.impl;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.zhangmeng.service.Request;
import com.zhangmeng.utils.ByteBufferUtil;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @author zm
 * @date 2025/2/22 16:13
 * @version: 1.0
 */
public class PingRouter extends BaseRouterImpl {

    private final Log log = LogFactory.get();

//    @Override
//    public void PreHandle(Request request) {
//        log.info("Call Router PreHandle");
//        Socket socket = request.GetConnection().GetTCPConnection();
//
//        try {
//            OutputStream outputStream = socket.getOutputStream();
//            outputStream.write("before ping ....".getBytes());
//            outputStream.flush();
//        } catch (IOException e) {
////            throw new RuntimeException(e);
//            log.error("call back ping ping ping error");
//        }
//
//    }

    @Override
    public void Handle(Request request) {
        log.info("Call Router Handle");
        Socket socket = request.GetConnection().GetTCPConnection();
        try {
            OutputStream outputStream = socket.getOutputStream();
            InputStream inputStream = socket.getInputStream();
            log.info("recv from client : msgId={}, data={}", request.GetMsgID(),  new String(request.GetData()));
            ByteBufferUtil.sendMsg(1, "ping ping ping\n".getBytes(), outputStream);
        } catch (IOException e) {
//            throw new RuntimeException(e);
            log.error("call back ping ping ping error");
        }
    }

//    @Override
//    public void PostHandle(Request request) {
//        log.info("Call Router PostHandle");
//        Socket socket = request.GetConnection().GetTCPConnection();
//        try {
//            OutputStream outputStream = socket.getOutputStream();
//            outputStream.write("after ping ....".getBytes());
//            outputStream.flush();
//        } catch (IOException e) {
////            throw new RuntimeException(e);
//            log.error("call back ping ping ping error");
//        }
//    }
}
